package test.com.sms.selenium.singlepoint.util;

import org.openqa.selenium.By;

import test.com.sms.selenium.framework.SeleniumPageUtil;
import test.com.sms.selenium.util.SeleniumUtil;

public class SpSROSearchPageUtil extends SeleniumPageUtil{

	public final static By SRO_SPAN_HEADER = By.id("ctl44_hdrMain_lblHeader");
	public final static By SRO_LINK_SEARCH = By.xpath("//*[@id='ddcf634a-a5bf-4f11-9467-6e6d8e050e40']/a");
	public final static By SRO_LINK_CLEAR = By.xpath("//*[@id='dc8aa588-ce68-459b-a3a0-a7f5183bd3da']/a");
	public final static By SRO_TD_SRO_NUMBER = By.xpath("//*[@id='form1']/table/tbody/tr[2]/td/div/table[2]/tbody/tr[1]/td[1]");
	public final static By SRO_INPUT_SRO_NUMBER = By.id("txtSroNumber");
	public final static By SRO_TD_INCIDENT_NUMBER = By.xpath("//*[@id='form1']/table/tbody/tr[2]/td/div/table[2]/tbody/tr[1]/td[3]");
	public final static By SRO_INPUT_INCIDENT_NUMBER = By.id("txtIncidentNumber");
	public final static By SRO_TD_ASSIGNED_TO = By.xpath("//*[@id='form1']/table/tbody/tr[2]/td/div/table[2]/tbody/tr[1]/td[5]");
	public final static By SRO_INPUT_ASSIGNED_TO = By.id("ctl44_txtCreatedBy");
	public final static By SRO_ASSIGNED_TO_LIST = By.xpath("//*[@id='ctl44_aceCreatedBy_completionListElem']");
	public final static By SRO_TD_P_O_NUMBER = By.xpath("//*[@id='form1']/table/tbody/tr[2]/td/div/table[2]/tbody/tr[1]/td[7]");
	public final static By SRO_SELECT_P_O_NUMBER = By.id("ctl44_cboPurchaseOrder");
	public final static By SRO_INPUT_P_O_NUMBER = By.id("ctl44_txtPONumber");
	public final static By SRO_TD_WAREHOUSE = By.xpath("//*[@id='form1']/table/tbody/tr[2]/td/div/table[2]/tbody/tr[2]/td[1]");
	public final static By SRO_INPUT_WAREHOUSE = By.id("ctl44_txtWarehouse");
	public final static By SRO_TD_CUSTOMER = By.xpath("//*[@id='form1']/table/tbody/tr[2]/td/div/table[2]/tbody/tr[2]/td[3]");
	public final static By SRO_INPUT_CUSTOMER = By.id("ctl44_txtCustomer");
	public final static By SRO_TD_SRO_TYPE = By.xpath("//*[@id='form1']/table/tbody/tr[2]/td/div/table[2]/tbody/tr[2]/td[5]");
	public final static By SRO_SELECT_SRO_TYPE = By.id("ctl44_cboSroType");
	public final static By SRO_TD_SRO_STATUS = By.xpath("//*[@id='form1']/table/tbody/tr[2]/td/div/table[2]/tbody/tr[2]/td[7]");
	public final static By SRO_SELECT_SRO_STATUS = By.id("ctl44_cboSROStat");
	
	public final static By SRO_TD_OPENED_DATE_BETWEEN = By.xpath("//*[@id='form1']/table/tbody/tr[2]/td/div/table[2]/tbody/tr[3]/td[1]");
	public final static By SRO_INPUT_OPENED_START_DATE = By.id("ctl44_dtmOpenStartDate_txtDatePart");
	public final static By SRO_IMG_OPENED_START_DATE = By.id("ctl44_dtmOpenStartDate_imgDateCalendar");
	public final static By SRO_DIV_OPENED_START_DATE_CLOSE = By.id("ctl44_dtmOpenStartDate_txtDatePart_CalendarExtender_close");
	public final static By SRO_IMG_OPENED_START_CLEAR_DATE = By.id("ctl44_dtmOpenStartDate_imgClearDate");
	public final static By SRO_INPUT_OPENED_END_DATE = By.id("ctl44_dtmOpenEndDate_txtDatePart");
	public final static By SRO_IMG_OPENED_END_DATE = By.id("ctl44_dtmOpenEndDate_imgDateCalendar");
	public final static By SRO_DIV_OPENED_END_DATE_CLOSE = By.id("ctl44_dtmOpenEndDate_txtDatePart_CalendarExtender_close");
	public final static By SRO_IMG_OPENED_END_CLEAR_DATE = By.id("ctl44_dtmOpenEndDate_imgClearDate");
	
	public final static By SRO_TD_CLOSED_DATE_BETWEEN = By.xpath("//*[@id='form1']/table/tbody/tr[2]/td/div/table[2]/tbody/tr[4]/td[1]");
	public final static By SRO_INPUT_CLOSED_START_DATE = By.id("ctl44_dtmClosedStartDate_txtDatePart");
	public final static By SRO_IMG_CLOSED_START_DATE = By.id("ctl44_dtmClosedStartDate_imgDateCalendar");
	public final static By SRO_DIV_CLOSED_START_DATE_CLOSE = By.id("ctl44_dtmClosedStartDate_txtDatePart_CalendarExtender_close");
	public final static By SRO_IMG_CLOSED_START_CLEAR_DATE = By.id("ctl44_dtmClosedStartDate_imgClearDate");
	public final static By SRO_INPUT_CLOSED_END_DATE = By.id("ctl44_dtmClosedEndDate_txtDatePart");
	public final static By SRO_IMG_CLOSED_END_DATE = By.id("ctl44_dtmClosedEndDate_imgDateCalendar");
	public final static By SRO_DIV_CLOSED_END_DATE_CLOSE = By.id("ctl44_dtmClosedEndDate_txtDatePart_CalendarExtender_close");
	public final static By SRO_IMG_CLOSED_END_CLEAR_DATE = By.id("ctl44_dtmClosedEndDate_imgClearDate");
	
	public final static By SRO_IMG_REFRESH = By.id("ctl44_SroSearch_Header_imgRefresh");
	public final static By SRO_SPAN_RESULTS = By.id("ctl44_SroSearch_ReportOutput");
	
	// 'Opened Date Between' & 'Closed Date Between' date
	public final static By SRO_DIV_OPENED_START_DATE = By.id("ctl44_dtmOpenStartDate_txtDatePart_CalendarExtender_day_0_3");
	public final static By SRO_DIV_OPENED_END_DATE = By.id("ctl44_dtmOpenEndDate_txtDatePart_CalendarExtender_day_3_2");
	public final static By SRO_DIV_CLOSED_START_DATE = By.id("ctl44_dtmClosedStartDate_txtDatePart_CalendarExtender_day_1_3");
	public final static By SRO_DIV_CLOSED_END_DATE = By.id("ctl44_dtmClosedEndDate_txtDatePart_CalendarExtender_day_4_3");
	/*SRO results*/
	public final static By SRO_SPAN_SRO_RESULT = By.id("ctl44_SroSearch_ReportOutput");
	public final static By SRO_LINK_FIRST_RESULT = By.xpath("//*[@id='ctl44_SroSearch_ReportOutput']/table/tbody/tr[3]/td[1]/a");
	// element of search results.
	public final static By SRO_RESULT_TH_CUSTOMER_TICKENT_NUMBER = By.id("ctl44_SroSearch_sort_14");
	// element of SRO search result edit page.
	public final static By SROSE_SPAN_CUSTOMER_TICKET = By.id("ctl44_lblCustomerTicketNumber");
	
	public static void enterSROSearchPage() {
		SpHomePageUtil.enter();
		SpHomePageUtil.enterSROSearchPage();
	}
	public static void enterSROSearch() {
		SpHomePageUtil.enter();
		SpHomePageUtil.changeCustomer("SMS MASTER");
		SeleniumUtil.waitForFixedMilliseconds(5000);
		SpHomePageUtil.changeUser("ahefele@sysmaint.com");
		SpHomePageUtil.enterSROSearchPage();
	}

	public static void waitForPageToLoad() {
		SeleniumUtil.selectDefaultContent();
		SeleniumUtil.selectFrame(SpHomePageUtil.HOME_FRAME_CONTENT_NAME);
		SeleniumUtil.waitOnElements(new By[]{
			SRO_LINK_SEARCH, SRO_LINK_CLEAR, SRO_SELECT_P_O_NUMBER, SRO_SELECT_SRO_TYPE, SRO_SELECT_SRO_STATUS, SRO_IMG_OPENED_START_DATE,
			SRO_IMG_OPENED_START_CLEAR_DATE, SRO_IMG_OPENED_END_DATE, SRO_IMG_OPENED_END_CLEAR_DATE, SRO_IMG_CLOSED_START_DATE, 
			SRO_IMG_CLOSED_START_CLEAR_DATE, SRO_IMG_CLOSED_END_DATE, SRO_IMG_CLOSED_END_CLEAR_DATE
		});
		SeleniumUtil.isDisplayed(SeleniumUtil.findElement(SRO_LINK_SEARCH));
		SeleniumUtil.isDisplayed(SeleniumUtil.findElement(SRO_LINK_CLEAR));
		SeleniumUtil.isDisplayed(SeleniumUtil.findElement(SRO_SELECT_P_O_NUMBER));
		SeleniumUtil.isDisplayed(SeleniumUtil.findElement(SRO_SELECT_SRO_TYPE));
		SeleniumUtil.isDisplayed(SeleniumUtil.findElement(SRO_SELECT_SRO_STATUS));
		SeleniumUtil.isDisplayed(SeleniumUtil.findElement(SRO_IMG_OPENED_START_DATE));
		SeleniumUtil.isDisplayed(SeleniumUtil.findElement(SRO_IMG_OPENED_START_CLEAR_DATE));
		SeleniumUtil.isDisplayed(SeleniumUtil.findElement(SRO_IMG_OPENED_END_DATE));
		SeleniumUtil.isDisplayed(SeleniumUtil.findElement(SRO_IMG_OPENED_END_CLEAR_DATE));
		SeleniumUtil.isDisplayed(SeleniumUtil.findElement(SRO_IMG_CLOSED_START_DATE));
		SeleniumUtil.isDisplayed(SeleniumUtil.findElement(SRO_IMG_CLOSED_START_CLEAR_DATE));
		SeleniumUtil.isDisplayed(SeleniumUtil.findElement(SRO_IMG_CLOSED_END_DATE));
		SeleniumUtil.isDisplayed(SeleniumUtil.findElement(SRO_IMG_CLOSED_END_CLEAR_DATE));
	}
	public static void waitForResultsRefresh() {
		boolean flag = !SeleniumUtil.findElements(SRO_IMG_REFRESH).get(0).getAttribute("style").toLowerCase().contains("display: none");
		long before = System.currentTimeMillis();
		while(flag){
			if((System.currentTimeMillis() - before) > 30 * 1000){
				SeleniumUtil.fail("more than 30 seconds no response!");
				return;
			}else{
				flag = !SeleniumUtil.findElements(SRO_IMG_REFRESH).get(0).getAttribute("style").toLowerCase().contains("display: none");
			}
		}
	}
}
